package com.wan.base

import android.os.Bundle
import android.support.v4.app.Fragment
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

/**
 * todo 因为 setUserVisibleHint 比 onCreateView要执行的早,所以需要二次进行执行
 * @Description 懒加载碎片
 */
abstract class BaseLazyFragment : Fragment() {

    private val TAG = this.javaClass.name

    private var isFirstLoad: Boolean = false
    private var isPre: Boolean = false


    private fun lazyLoad() {

        if (!isFirstLoad && userVisibleHint && isPre) {
            Log.e(TAG,"==> 加载碎片")
            initData()
            isFirstLoad = true
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        Log.e(TAG, "onCreateView")
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Log.e(TAG, "onViewCreated")

    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        Log.e(TAG, "onActivityCreated")
        isPre = true
        lazyLoad()
    }


    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        lazyLoad()
    }

    override fun onStart() {
        super.onStart()
        Log.e(TAG, "onStart")

    }


    override fun onDetach() {
        super.onDetach()
        isFirstLoad = false
        isPre = false
    }


    abstract fun initData()

}